<?php

namespace App\Http\Controllers\Api;

use App\Jobs\SendReminderEmail;
use App\Models\AdvContent;
use App\Models\Article;
use App\Models\ArticleCategory;
use App\Models\ArticleOperateLog;
use App\Models\LiveMeetingRoom;
use App\Models\User;
use App\Models\UserRelations;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Storage;

class ArticleController extends Controller
{
    protected $pagesize;

    public function __construct(Request $request)
    {
        $this -> pagesize = $request -> input('pagesize') ?? 3;
    }


    /*
     * 通过父级获取子分类
     * */
    public function getChildCategory(Request $request){

        $parent_id = $request -> input('parent_id');
        $pagesize  = $request -> input('pagesize') ?? 1000;

        $res = ArticleCategory::where('parent_id',$parent_id);
        if($request -> filled('region_id')){
            $region_id = $request -> input('region_id');
            $res -> whereIn('region_id',[$region_id,0]);
        }else{
            $res -> where('region_id',0);
        }
		//category_sort
        $result = $res -> where('is_show',1) -> limit($pagesize)->orderBy('category_sort', 'asc')-> get(['id','category_name','category_img']);

        return jsonSuccess($result);
    }

    /**
     * 根据分类ID获得文章列表
     * @param Request $request
     * @return false|string
     */
    public function article_list(Request $request)
    {
        $categoryId  = $request->input('category_id');
        $articleType = $request->input('types'); // 0全部 1图文、视频文章， 2短视频文章
        $regionId = $request->input('region_id');
        $user['user_id'] = $request->input('user_id');
        $user['is_admin'] = $request->input('is_admin');
		$istop= $request->input('istop');
        $articleList = Article::get_article_list($articleType, $categoryId, $regionId, $this->pagesize, 1, $user,$istop);
		
        return jsonSuccess($articleList);
    }
 

    /**
     * Display a listing of the resource.
     *
     */
    public function index(Request $request)
    {
        $article = new Article();
        $articleData = $article -> article_data($request,$this -> pagesize);

        return jsonSuccess($articleData);
    }


    // 通过父级id， 获取子类文章
    public function getChildArticle(Request $request){


        if(!$request -> filled('pid') && !is_numeric($request -> input('pid'))){
            return jsonError(4011);
        }
        $pid       = $request -> input('pid');
        $region_id = $request -> input('region_id') ?: 0;


        $childArticle = Article::getChildArticle($pid, $region_id, $this -> pagesize);


        return jsonSuccess($childArticle);

    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     */
    public function details(Request $request)
    {
        $id = $request -> input('id');
        if($id && is_numeric($id)){

            // 阅读数自增
            Article::article_operated($id, '1');
            $article = Article::with(['articles_author:id,name,avatar,fans_num']) -> where('is_show',1) -> find($id);

            if($article){
                return jsonSuccess($article);
            }else{
                return jsonError('',4008,404);
            }
        }else{
            return jsonError(4020);
        }
    }

    // 文章阅读自增
    public function articleIncRead(Request $request){

        $article_id  = $request -> input('article_id');

        if($article_id && is_numeric($article_id)){

            $res = Article::article_operated($article_id, 1);

            return jsonSuccess();
        }else{
            return jsonError(4020);
        }
    }


    // 文章操作  点赞、喜欢、分享、收藏、作者关注
    public function articleOperated(Request $request){

        $id     = $request -> input('id');
        $type   = $request -> input('type');
        if($id && is_numeric($id)){


            $res = Article::article_operated($id, $type);
            if($res){
                return jsonSuccess();
            }else{
                return jsonError();
            }
        }else{
            return jsonError(4020);
        }
    }

    // 多图文章穿插展示
    public function articleImgVideo(Request $request){


        $article = Article::articleImgVideo($request, $this -> pagesize);


        return jsonSuccess($article);
    }


    public function getVideo(Request $request)
    {

        if(!$request -> filled('video_ids_token')){
            if(!$request -> filled('article_id')){
                return jsonError(4011);
            }
        }


        $article = new Article();
        $videoData = $article -> video_data($request);
        
        $video_ids_token = $videoData['video_ids_token'];
        unset($videoData['video_ids_token']);
        $resData = [];
        foreach ($videoData as $videoDataVal) {
            $resData[] = $videoDataVal;
        }


        return jsonSuccess(['video_list' => $resData, 'video_ids_token' => $video_ids_token]);
    }


    public function test(Request $request){

        if(!$request -> filled('video_info_token')){
            if(!$request -> filled('article_id')){
                return jsonError(4011);
            }
        }

        $offset = $request -> input('offset') ?: 5;
        $token  = $request -> input('video_info_token');
        $video_id = $request -> input('article_id');


        $video = new Article();
        $res = $video -> getVideoData($video_id,$offset,$token);

        return jsonSuccess($res);

    }

    public function ali_upload_test(Request $request)
    {
        return jsonSuccess([Storage::disk('oss') -> url('')]);
        dd(Storage::disk('api') -> url(''));

    }
	public function article_to_top(){
		$article = new Article();
        $res = $article ->rand_to_top(36,2);
		if($res){
			return jsonSuccess();
		}
	}

}
